<template>
  <div class="app-container">
    <el-card>
      <el-row style="flex" justify="space-between">
        <el-col :span="22">
          <el-form inline>
            <el-form-item>
              <el-input
                v-model.trim="queryData.tag_name"
                placeholder="标签名称"
                size="small"
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.subject_id"
                size="small"
                placeholder="归属学科"
                clearable
                @change="changeSubject"
                @clear="clearsub"
              >
                <el-option
                  v-for="item in treeData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.directory_id"
                size="small"
                placeholder="归属目录"
                clearable
                @clear="clearList"
              >
                <el-option
                  v-for="item in directoryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select v-model="queryData.is_show" size="small" placeholder="账号状态" clearable>
                <el-option label="显示" :value="1" />
                <el-option label="隐藏" :value="2" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                size="small"
                plain
                @click="onSubmit"
              >搜索</el-button>
              <el-button size="small" @click="onReset">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" plain @click="$refs.addForm.dialogShow=true">创建标签</el-button>
        </el-col>
      </el-row>
      <el-row>
        <div class="container">
          <!-- 表格区域 -->
          <el-table :data="list" border style="width: 100%">
            <el-table-column type="index" label="#" align="center" />
            <el-table-column prop="tag_name" label="标签名称" />
            <el-table-column prop="directory_name" label="归属目录" />
            <el-table-column prop="subject_name" label="归属学科" />
            <el-table-column prop="username" label="创建人" align="center" />
            <el-table-column prop="is_show" label="是否显示" align="center">
              <template slot-scope="{row}">
                <el-switch
                  v-model="row.is_show"
                  inactive-text="否"
                  active-text="是"
                  :active-value="1"
                  :inactive-value="2"
                  @change="show(row.id,row.is_show)"
                />
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="{ row }">
                <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-edit"
                  circle
                  @click="editRow(row.id)"
                />
                <el-button
                  type="danger"
                  size="small"
                  icon="el-icon-delete"
                  circle
                  @click="delRow(row.id)"
                />
              </template>
            </el-table-column>
          </el-table>
          <br>
          <!-- 分页区域 -->
          <el-row type="flex" justify="end">
            <el-pagination
              :current-page.sync="queryData.page"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="queryData.limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalNum"
              background
              @size-change="sizeChange"
              @current-change="currentChange"
            />
          </el-row>
          <!-- 新增、修改弹框区域 -->
          <addTag ref="addForm" :list="list" @updateList="initData" />
        </div>
      </el-row>
    </el-card>
  </div>
</template>
<script>
import { getTagList, getTagId, delTag, getTreeList, putIsShow } from '@/api/tag'
import addTag from '@/views/subject/components/add-tag'
export default {
  name: 'Tag',
  components: {
    addTag
  },
  data() {
    return {
      // 归属目录
      directoryList: [],
      // 归属科目
      treeData: [],
      list: [], // 表格数据
      totalNum: 0, // 总数
      // 列表的需要的参数
      queryData: {
        subject_id: '',
        directory_id: '',
        tag_name: '',
        is_show: '',
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    // 调用初始化
    this.initData()
    // 调用学科
    this.getTree()
  },
  methods: {
    // 清空学科
    clearsub() {
      // console.log(23)
      this.initData()
      this.directoryList = []
    },
    // 清空
    clearList() {
      // this.initData()
      this.directoryList = []
      // this.initData()
    },
    // 切换显示
    async show(id, isShow) {
      console.log(id, isShow)
      await putIsShow({ id: id, is_show: isShow })
      // console.log(res)
      this.$message.success('切换成功')
    },
    // 归属学科
    async getTree() {
      const res = await getTreeList()
      this.treeData = res
    },
    // 归属科目
    changeSubject(event) {
      // console.log(event)
      const res = this.treeData.find(item => {
        return item.id === event
      })
      // console.log(res)
      this.directoryList = res.children
    },
    // 初始化数据
    async initData() {
      // 发起请求
      const res = await getTagList(this.queryData)
      // 数据可能需要加工
      // 赋值
      console.log(res)
      this.totalNum = res.total
      this.list = res.list
    },
    // 搜索提交
    onSubmit() {
      // 页码变成1
      this.queryData.page = 1
      // 拉取列表
      this.initData()
    },
    // 搜索重置
    onReset() {
      // 重置搜索表单
      this.queryData = {
        subject_id: '',
        directory_id: '',
        tag_name: '',
        is_show: '',
        page: 1,
        limit: 10
      }
      // 拉取列表
      this.initData()
    },
    // 每页数量变化的时候
    sizeChange(newSize) {
      // newSize 最新的每页数量
      this.queryData.limit = newSize
      this.initData()
    },
    // 页码变化事件
    currentChange(newPage) {
      // newPage 最新的页码
      this.queryData.page = newPage
      this.initData()
    },
    // 修改事件
    async editRow(rowId) {
      // 获取改行的数据
      const res = await getTagId(rowId)
      // 赋给弹框里面的表单
      console.log(res)
      this.$refs.addForm.formData = res
      // 复杂点：一些负责的表单控件需要手动去回显
      // 打开弹框
      this.$refs.addForm.dialogShow = true
    },
    // 删除事件
    async delRow(rowId) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 发生删除请求
        delTag(rowId)
        // 提示成功
        // 边缘问题：如果不是第一页，且这一页只有一个，且删除成功，页码需要-1
        if (this.queryData.page > 1 && this.list.length === 1) {
          this.queryData.page--
        }
        // 列表更新
        this.initData()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped>
.container {
  padding: 30px;
}
</style>
